import toastComponent from './toast.vue'

const Toast = {}
Toast.install = function(Vue,options){
    if(Toast.installed){
        return;
    }
    let toastTpl = Vue.extend(toastComponent);

    toastTpl.prototype.close = function(){
        this.isShowToast = false
    }
    const tpl = new toastTpl();
    document.body.appendChild(tpl.$mount().$el);
    
    tpl.isShowToast = false

    let toastFn = function(obj){
        tpl.isShowToast = true
    
        if(obj){
            tpl.content = obj.content || "请稍等";
            tpl.toastWidth = obj.width || 150;
            tpl.toastPosition = obj.position || "center";
            tpl.iconType = obj.iconType || "none";
            tpl.time = obj.time || 2000;
            tpl.toastShape = tpl.iconType?"rect":(obj.shape || "square")
        }
        let timeFun = setTimeout(()=>{
            tpl.close()
            clearTimeout(timeFun)
        },tpl.time)
    }

    Vue.prototype.$showToast = toastFn
}

export default Toast
